<?php
namespace Common\ORG;

class Page {

    // 分页栏每页显示的页数
    public $rollPage = 7;
    // 页数跳转时要带的参数
    public $parameter;
    // 分页URL地址
    public $url = '';
    // 默认列表每页显示行数
    public $listRows = 20;
    // 起始行数
    public $firstRow;
    // 分页总页面数
    protected $totalPages;
    // 总行数
    protected $totalRows;
    // 当前页数
    public $nowPage;
    // 分页的栏的总页数
    protected $coolPages;
    // 默认分页变量名
    public $varPage;
    // 分页显示定制
	protected $config = array('header'=>'条记录','prev'=>'&laquo;上一页','next'=>'下一页&raquo;','first'=>'首页','last'=>'末页','theme'=>' <div class="span12"><div class="dataTables_info">共%totalRow% %header% %nowPage%/%totalPage% 页</div></div><div class="span12 center"><div class="pagination"><ul>%first% %upPage% %linkPage% %downPage% %end% %go%</ul></div></div>%pagesize%');

    /**
     * 架构函数
     * @access public
     * @param array $totalRows  总的记录数
     * @param array $listRows   每页显示记录数
     * @param array $parameter  分页跳转的参数
     */
    public function __construct($totalRows,$listRows='',$parameter='',$url='' ) {
        $this->totalRows    =   $totalRows;
        $this->parameter    =   $parameter;
        $this->varPage      =   C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
		$this->varPsize      =   C('VAR_PAGESIZE') ? C('VAR_PAGESIZE') : 'psize' ;	//页面传过来的页数
		
		
		if( $listRows )
			$this->nowPsize = intval( $listRows );
		else
			$this->nowPsize = 20;
		
		if( !empty($_GET[$this->varPsize]) )
		{
			if(preg_match('/^(\d+)$/i' , $_GET[$this->varPsize] ) )
				$this->nowPsize      =   intval($_GET[$this->varPsize]);
			else
			{
				$_url = $_GET[$this->varPsize];
				$_url=str_replace(array(__URL__,C('TMPL_FILE_DEPR')),array('','/'), $_url);
				if( preg_match("#".$this->varPsize.C('TMPL_FILE_DEPR')."(\d+)#is", $_url, $match) )
				{
					$this->nowPsize = intval( $match[1]);
				}
			}
		}
		$this->listRows     = 	$this->nowPsize ;
		
        $this->totalPages   =   ceil($this->totalRows/$this->listRows);     		//总页数
        $this->coolPages    =   ceil($this->totalPages/$this->rollPage);
        $this->nowPage      =   !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
		
        if($this->nowPage<1){
            $this->nowPage  =   1;
        }elseif(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage  =   $this->totalPages;
        }
        $this->firstRow     =   $this->listRows*($this->nowPage-1);
    }

    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name]    =   $value;
        }
    }

    public function getTotalPages(){
        return $this->totalPages;
    }
    /**
     * 分页显示输出
     * @access public
	 * @param $sshow_psize      是否显示每页页数
     */
    public function show( $sshow_psize = FALSE) {
        if(0 == $this->totalRows) return '';
        $p              =   $this->varPage;
		$psize          =	$this->varPsize;
        $nowCoolPage    =   ceil($this->nowPage/$this->rollPage);

        // 分析分页参数
        if($this->url){
            $depr       =   C('URL_PATHINFO_DEPR');
            $url        =   rtrim(U('/'.$this->url,'',false),$depr).$depr.'__PAGE__'.$depr.'__PSIZE__';
        }else{
            if($this->parameter && is_string($this->parameter)) {
                parse_str($this->parameter,$parameter);
            }elseif(is_array($this->parameter)){
                $parameter      =   $this->parameter;
            }elseif(empty($this->parameter)){
                unset($_GET[C('VAR_URL_PARAMS')]);
                $var =  !empty($_POST)?$_POST:$_GET;
                if(empty($var)) {
                    $parameter  =   array();
                }else{
                    $parameter  =   $var;
                }
            }
            $parameter[$p]  =   '__PAGE__';
			if( $sshow_psize ){
				$parameter[$psize] = '__PSIZE__';
			}
            $url            =   U('',$parameter);
        }
        //上下翻页字符串
        $upRow          =   $this->nowPage-1;
        $downRow        =   $this->nowPage+1;
        //上一页，首页
		if ($upRow>0){
            $upPage     =   "<li class='prev'><a href='".str_replace('__PAGE__',$upRow,$url)."'>".$this->config['prev']."</a></li>";
            $theFirst   =   "<li class='prev'><a href='".str_replace('__PAGE__',1,$url)."' >".$this->config['first']."</a></li>";
        }elseif (1 < $this->totalPages){
            $upPage     =   '<li class="prev disabled"><a href="#">'.$this->config['prev'].'</a></li>';
            $theFirst   =   '<li class="prev disabled"><a href="#">'.$this->config['first'].'</a></li>';
		}else{
            $upPage     =   '<li class="prev disabled"><a >'.$this->config['prev'].'</a></li>';
            $theFirst   =   '<li class="prev disabled"><a href="#">'.$this->config['first'].'</a></li>';
        }
		//未页，下一页
        if ($downRow <= $this->totalPages){
            $theEndRow  =   $this->totalPages;
            $downPage   =   "<li class='next'><a href='".str_replace('__PAGE__',$downRow,$url)."'>".$this->config['next']."</a><li>";
            $theEnd     =   "<li class='next'><a href='".str_replace('__PAGE__',$theEndRow,$url)."' >".$this->config['last']."</a></li>";
        }elseif (1 < $this->totalPages){
            $downPage   =   '<li class="next disabled"><a href="#">'.$this->config['next'].'</a></li>';
            $theEnd     =   '<li class="next disabled"><a href="#">'.$this->config['last'].'</a></li>';
		}else{
            $downPage   =   '<li class="next disabled"> <a href="#">'.$this->config['next'].'</a></li>';
            $theEnd     =   '<li class="next disabled"> <a href="#">'.$this->config['last'].'</a></li>';
        }
        //页数 1 2 3 4 5 6 7
		$prePage = '';
		$nextPage = '';
        $linkPage = "";
        for($i=1;$i<=$this->rollPage;$i++){
            $page = ($this->nowPage - intval($this->rollPage/2)) <= 0 ? $i : $this->nowPage - intval($this->rollPage/2) + $i-1;
            $page = (($this->totalPages - $this->nowPage) >= intval($this->rollPage/2) || ($this->totalPages < $this->rollPage)) ? $page : $this->totalPages - $this->rollPage  + $i;
            $pageSize = $this->nowPsize;
			if($page!=$this->nowPage){
                if($page<=$this->totalPages){
                    $linkPage .= "<li><a href='".str_replace(array('__PAGE__','__PSIZE__'),array($page,$pageSize),$url)."'>".$page."</a></li>";
                }else{
                    break;
                }
            }else{
                if($this->totalPages != 1){
                    $linkPage .= "<li class='active' ><a href='#'>".$page."</a></li>";
                }
            }
        }
        $thego = '<li class="dataTables_length">&nbsp;&nbsp;&nbsp;&nbsp;跳转至第<select style="width:auto;height:35px;" name="'.$this->varPage.'" onchange="go_page'.$this->varPage.'(this.value)">';
        for($i=1;$i<=$this->totalPages;$i++){
            $thego .= '<option value ="'.str_replace(array("__PAGE__","__PSIZE__"),array($i,$this->nowPsize),$url).'" ';
            $thego .= $i == $this->nowPage ? 'selected="selected"' :'';
            $thego .= '>'.$i.'</option>';
        }
        $thego .= '</select>页<script type="text/javascript">
					function go_page'.$this->varPage.'(page){
						window.location = page;
					}
					</script></li>';
		
		//是否显示每页页数
		if( $sshow_psize )
		{
			$pagesize = '<div class="span2">每页页数<select style="width:auto;display:inline-block;" name="'.$this->varPsize.'" onchange="go_page'.$this->varPsize.'(this.value)">';
			if( !in_array($this->nowPsize, array(20,50,100,200,500)) )
			{
				$pagesize .= '<option value ="'.str_replace(array("__PAGE__","__PSIZE__"),array($this->nowPage,$this->nowPsize),$url).'" ';
				$pagesize .= '>'.$this->nowPsize.'</option>';
			}
			$pagesize .= '<option value ="'.str_replace(array("__PAGE__","__PSIZE__"),array($this->nowPage,20),$url).'" ';
			$pagesize .= 20 == $this->nowPsize ? 'selected="selected"' :'';
			$pagesize .= '>20</option>';
			$pagesize .= '<option value ="'.str_replace(array("__PAGE__","__PSIZE__"),array($this->nowPage,50),$url).'" ';
			$pagesize .= 50 == $this->nowPsize ? 'selected="selected"' :'';
			$pagesize .= '>50</option>';
			$pagesize .= '<option value ="'.str_replace(array("__PAGE__","__PSIZE__"),array($this->nowPage,100),$url).'" ';
			$pagesize .= 100 == $this->nowPsize ? 'selected="selected"' :'';
			$pagesize .= '>100</option>';
			$pagesize .= '<option value ="'.str_replace(array("__PAGE__","__PSIZE__"),array($this->nowPage,200),$url).'" ';
			$pagesize .= 200 == $this->nowPsize ? 'selected="selected"' :'';
			$pagesize .= '>200</option>';
			$pagesize .= '<option value ="'.str_replace(array("__PAGE__","__PSIZE__"),array($this->nowPage,500),$url).'" ';
			$pagesize .= 500 == $this->nowPsize ? 'selected="selected"' :'';
			$pagesize .= '>500</option>';
			$pagesize .= '</select><script type="text/javascript">
			function go_page'.$this->varPsize.'(page){
				window.location = page;
			}
			</script></div>';
		}
		else
		{
			$pagesize='';
		}
		
        $pageStr     =   str_replace(
            array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%','%go%','%pagesize%'),
            array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd,$thego,$pagesize),$this->config['theme']);
        return $pageStr;
    }
}